<?php

/**
 * Description of ContattiController
 *
 * @author luigi.pastore
 * @created  16-mag-2011
 */
class ContattiController extends Classes_Controller
{
    public function init()
    {
	$this->model_contact	= new Classes_Contact();
	$this->model_province	= new Classes_Province();
	
	$this->contact_roles = $this->application->getResource( 'ContactRoles' );
	
	$this->layout->province = $this->model_province->fetchAll();
	$this->layout->contact_roles = $this->contact_roles;
	
        // Dati Form
        $this->form_data = $this->getParams();
        
	// Edit...
	if( is_numeric( $id = $this->getParam( 'id' ) ) )
	{
	    // Fetch
	    $contact = $this->model_contact->fetchById( array( $id ) );

	    if( empty( $contact ) )
		trigger_error( 'Nessuna contact' );
	    
	    $this->contact = array_shift( $contact );

            // Se sto editando precompilo la sessione
            if( !empty( $this->form_data['salva'] ) )
                $this->contact = array_merge( $this->contact, $this->form_data );

            # Assegniamo i dati al layout
        
            // Format..
            list( $this->contact['contact_phone_prefix'], $this->contact['contact_phone_number'] ) = explode( '/', $this->contact['contact_phone'] );
            list( $this->contact['contact_work_prefix'], $this->contact['contact_work_number'] ) = explode( '/', $this->contact['contact_work'] );
            list( $this->contact['contact_mobile_prefix'], $this->contact['contact_mobile_number'] ) = explode( '/', $this->contact['contact_mobile'] );
            list( $this->contact['contact_fax_prefix'], $this->contact['contact_fax_number'] ) = explode( '/', $this->contact['contact_fax'] );            
            
	    $this->layout->data = $this->contact;

	    $this->layout->contact_id = $this->contact['contact_id'];
	}            
    }
    
    public function indexAction()
    {
	$this->layout->content = array( 'script' => 'artisti/index' );
    }
    
    public function infoAction()
    {
        if( $this->form_data['salva'] AND $this->form_data['salva'] !== '' )
	{
	    $this->layout->data = $this->form_data;

            // Mapping...
            $data = array(
                'contact_id'            => $this->contact['contact_id'],
                'contact_firstname'	=> ucfirst( $this->form_data['contact_firstname'] ),
                'contact_lastname'	=> ucfirst( $this->form_data['contact_lastname'] ),
                'contact_email'		=> $this->form_data['contact_email'],
                'contact_phone'		=> $this->form_data['contact_phone_prefix'] .'/'. $this->form_data['contact_phone_number'],
                'contact_email2'	=> $this->form_data['contact_email2'],
                'contact_email3'	=> $this->form_data['contact_email3'],
                'contact_work'		=> $this->form_data['contact_work_prefix'] .'/'. $this->form_data['contact_work_number'],
                'contact_mobile'	=> $this->form_data['contact_mobile_prefix'] .'/'. $this->form_data['contact_mobile_number'],
                'contact_fax'		=> $this->form_data['contact_fax_prefix'] .'/'. $this->form_data['contact_fax_number'],
                'contact_city'		=> $this->form_data['contact_city'],
                'contact_cap'		=> $this->form_data['contact_cap'],
                'contact_loc'		=> $this->form_data['contact_loc'],
                'contact_street'	=> $this->form_data['contact_street'],
                'contact_street_number'	=> $this->form_data['contact_street_number'],
                'contact_province_id'	=> $this->form_data['contact_province_id'],		    
                'contact_notes'		=> $this->form_data['contact_notes']
            );

            // Edit o Aggiunta?
            if( empty( $this->contact ) )
                $data['contact_createdby'] = $this->application->auth->user['user_id'];
            else
                $data['contact_modifiedby'] = $this->application->auth->user['user_id'];

            $model = new Classes_Contact( $data );

            if( is_numeric( $contact_id = $model->save()->contact_id ) )
            {
                // Rimanda alla pagina di conferma e cancella file temporanei...
                //$this->layout->forward = 'contatti/rubrica';
                // Redirect...
                return header( 'Location: /admin/contatti/riepilogo/id/' . $contact_id );
            }
            else
            {
                $errorMsg = $model->errors;

                if( $model->errors['contact_firstname'] OR $model->errors['contact_lastname'] )
                    $errorMsg['contact_name'] = ERR_EMPTY;

                if( $model->errors['contact_city'] )
                    $errorMsg['contact_address'] = sprintf( ERR_EMPTY_FIELD, 'Citt&agrave;' );

                if( $model->errors['contact_cap'] )
                    $errorMsg['contact_address'] = sprintf( ERR_FORMAT, 'Cap' );

                if( $model->errors['contact_province_id'] )
                    $errorMsg['contact_address'] = sprintf( ERR_EMPTY_SELECT, 'Provincia' );
            }          
	}
                
        $this->layout->errorMsg = $errorMsg;
	$this->layout->content	= array( 'script' => 'contatti/info' );
    }
    
    public function riepilogoAction()
    {
	if( empty( $this->contact ) )
	    trigger_error( 'Nessun contatto Selezionato' );
        
        # Formattiamo campi

        # Assegniamo i dati al layout
        $this->layout->content = array( 'script' => 'contatti/riepilogo' );
    }    
    
    public function vediAction()
    {
	if( is_null( $this->contact ) )
	{
	    trigger_error( 'Errore Id Contatto' );
	}
		
	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'contatti/vedi' );
    }

    public function associaAction()
    {
	// Se non c'è il post lancia l'errore
	if( !is_array( $this->contact ) )
            return header( 'Location: /admin/contatti/' );
        
        $this->layout->content = array( 'script' => 'contatti/associa' );
    }

    public function associalocationAction()
    {
	$this->model_location	= new Classes_Location();
	$this->locations	= $this->model_location->fetchAll();

	// Se non c'è il post lancia l'errore
	if( !is_array( $this->contact ) )
            return header( 'Location: /admin/contatti/info' );

	if( $this->form_data['salva'] AND $this->form_data['salva'] !== '' )
	{
	    $roles = array();
	    
            if( count( $this->form_data['locations'] ) > 0 )
            {
                foreach( $this->form_data['locations'] AS $key => $value )
                {
                    foreach( $value as $role ){
                        // Mapping...
                        $roles[] = array( 'fab_locations_location_id' => $key, 'fab_contacts_role' => $role );
                        
                    }
                }
            }
	    
	    if( !$errorMsg )
	    {
		$model = new Classes_Contact( array( 'contact_id' => $this->contact['contact_id'] ) );
		
                if( is_numeric( $contact_id = $model->saveExtra( array( 'locations' => $roles ) )->contact_id ) AND !$model->errors )
                {
                    // Redirect...
                    return header( 'Location: /admin/contatti/riepilogo/id/' . $contact_id );
                }
                else
                {
                    if( $model->errors )
                        $errorMsg = $model->errors;
                }
	    }
	}

	$this->layout->errorMsg	    = $errorMsg;
	$this->layout->locations    = $this->locations;
	$this->layout->roles	    = $this->roles;
	$this->layout->content	    = array( 'script' => 'contatti/associa-location' );		
    }
    
    public function associapartnerAction()
    {
	$this->model_partner	= new Classes_Partner();
	$this->partners	= $this->model_partner->fetchAll();

	// Se non c'è il post lancia l'errore
	if( !is_array( $this->contact ) )
            return header( 'Location: /admin/contatti/info' );

	if( $this->form_data['salva'] AND $this->form_data['salva'] !== '' )
	{
	    $roles = array();
	    
            if( count( $this->form_data['partners'] ) > 0 )
            {
                foreach( $this->form_data['partners'] AS $key => $value )
                {
                    foreach( $value as $role ){
                        // Mapping...
                        $roles[] = array( 'fab_partners_partner_id' => $key, 'fab_contacts_role' => $role );
                        
                    }
                }
            }
	    
	    if( !$errorMsg )
	    {
		$model = new Classes_Contact( array( 'contact_id' => $this->contact['contact_id'] ) );
		
                if( is_numeric( $contact_id = $model->saveExtra( array( 'partners' => $roles ) )->contact_id ) AND !$model->errors )
                {
                    // Redirect...
                    return header( 'Location: /admin/contatti/riepilogo/id/' . $contact_id );
                }
                else
                {
                    if( $model->errors )
                        $errorMsg = $model->errors;
                }
	    }
	}

	$this->layout->errorMsg	    = $errorMsg;
	$this->layout->partners     = $this->partners;
	$this->layout->roles	    = $this->roles;
	$this->layout->content	    = array( 'script' => 'contatti/associa-partner' );		
    }

    public function rubricaAction()
    {
	$this->layout->count_contacts = $this->model_contact->getCount();
	$this->layout->content = array( 'script' => 'contatti/rubrica' );
    }
} 

